package com.town.garden;

import com.town.garden.propertites.NvrProperties;
import com.town.garden.service.impl.NvrLoginServiceImpl;
import com.town.garden.storage.NvrSessionStorage;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.env.Environment;

/**
 * @Description 系统启动类
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/5/13
 */
@SpringBootApplication
@EnableConfigurationProperties(value = NvrProperties.class)
@Slf4j
public class ApplicationNvrServerRunner implements ApplicationRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware {
    @Autowired
    private ApplicationContext context;

    @Autowired
    private NvrLoginServiceImpl nvrLoginService;
    @Autowired
    private StringEncryptor codeSheepEncryptorBean;

    public static void main(String[] args) {
        SpringApplication.run(ApplicationNvrServerRunner.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        nvrLoginService.handlerOperation();
    }

    /**
     * 打印配置文件字段加密信息
     */
    private void printProperties() {
        Environment environment = context.getBean(Environment.class);
        // 首先获取配置文件里的原始明文信息
        String password = environment.getProperty("town.garden.nvr.camera-default-password");
        // 加密
        String encryptPassword = encrypt(password);
        log.info("原始明文密码为：{}", password);
        log.info("====================================");
        log.info("原始明文密码加密后的结果为：{}", encryptPassword);
    }

    /**
     * 加密
     *
     * @param originPassord
     * @return
     */
    private String encrypt(String originPassord) {
        String encryptStr = codeSheepEncryptorBean.encrypt(originPassord);
        return encryptStr;
    }

    /**
     * 解密
     *
     * @param encryptedPassword
     * @return
     */
    private String decrypt(String encryptedPassword) {
        String decryptStr = codeSheepEncryptorBean.decrypt(encryptedPassword);
        return decryptStr;
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        this.nvrLoginService.logout();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        NvrSessionStorage.applicationContext = applicationContext;
    }
}
